package com.github.baichuan.web.servlet;

import com.github.baichuan.web.dto.Result;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

public class Restful {
    /**
     * 将创建操作结果转换为RESTful应答
     */
    public static ResponseEntity<Result> created(Result result){
        if(result.isSuccess()) {
            return ResponseEntity.created(null).body(result);
        } else {
            return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).body(result);
        }
    }

    /**
     * 将删除操作结果转换为RESTful应答
     */
    public static ResponseEntity<Result> deleted(Result result){
        if(result.isSuccess()) {
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).body(result);
        }
    }

    /**
     * 将操作结果转换为RESTful应答
     */
    public static ResponseEntity<Result> response(Result result){
        if(result.isSuccess()) {
            return ResponseEntity.ok(result);
        } else {
            return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).body(result);
        }
    }

    /**
     * 返回验证错误
     */
//    public static ResponseEntity badRequest(BindingResult result){
//        Map<String, String> errors = ValidationUtils.message(result);
//        return ResponseEntity.badRequest().body(errors);
//    }
}
